﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using System.Web.Script.Serialization;
namespace WcfServiceLibrary2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
    public class Service1 : IService1
    {
        public string GetData(String value)
        {
            
            return string.Format("You entered: {0}", value);
        }

        


       




        public ImageData GetImageData(string path)
        {
            return ImageData.GetImage(path); 
        }


        public List<ImageData> GetImagePath()
        {
            return ImageData.GetImagePath();
        }


        public string GetByteFromString(byte[] data)
        {
           try
           {
               return Encoding.UTF8.GetString(data, 0, data.Length);
           }
           catch (System.Exception ex)
           {
               throw new FaultException(ex.Message);
           }
        }


        public string GetStringFromStream(FileData fileData)
        {
           try
           {
               byte[] data = new byte[fileData.fileStream.Length];
               fileData.fileStream.Read(data, 0, (int)fileData.fileStream.Length);
               return Encoding.UTF8.GetString(data);
           }
           catch (System.Exception ex)
           {
               throw new FaultException(ex.Message);
           }
        }





        public void MoveFile(string destinationFileName, byte[] fileData)
        {
            try
            {
                
                string folderName = @"D:\\vanhung";
                if (!Directory.Exists(folderName))
                {
                    Directory.CreateDirectory(folderName);
                }
                string fileNames = Path.Combine(folderName, destinationFileName);
                if (!File.Exists(fileNames))
                {
                    
                    FileStream fileStream = File.Create(fileNames);
                    fileStream.Write(fileData, 0, fileData.Length);
                    fileStream.Close();
                }
            }
            catch (System.Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }


        public Stream SendStream(string data)
        {
           try
           {
               byte[] databyte = Encoding.UTF8.GetBytes(data);
               MemoryStream stream = new MemoryStream(databyte);
               return stream;
           }
           catch (System.Exception ex)
           {
               throw new FaultException(ex.Message);
           }
        }



        public string GetStringByStream(Stream data)
        {
            byte[] dataByte = new byte[(int)data.Length];
            return Encoding.UTF8.GetString(dataByte, 0, dataByte.Length);
        }


        //public List<SinhVien> SinhVienGetAll()
        //{
        //    return SinhVien.SinhVienGetAll();
        //}


       

        public byte[] SendImage(byte[]data)
        {
           return ImageData.SendImage(data);
        }


        public void ImageInsert(Image newImage)
        {
            Image.ImageInsert(newImage);
        }


        public Image ImageGetByID(int id)
        {
            return Image.ImageGetById(id);
        }


        public List<Image> ImageGetAll()
        {
            return Image.ImageGetAll();
        }






        //public List<SinhVien> GetAll()
        //{
        //    return SinhVien.GetAll();
        //}


        public void Configgurations(string user, string password, string database, string datasource)
        {
            Configguration.SaveConfig(user, database, password, datasource);
        }

        int i = 0;
        public int demsonguoiconnect()
        {
            i++;
            return i;
        }


        public void DoSomething()
        {
            i++;
        }


        





        


        


        public void insertAnh(Anh anh)
        {
            Anh.InsertAnh(anh);
        }




        public List<Anh> GetAllAnh()
        {
            return Anh.GetAllAnh();
        }

        public Anh GetAnhById(int id)
        {
          return  Anh.GetImageById(id);
            //throw new NotImplementedException();
        }


        public System.Collections.ObjectModel.ObservableCollection<SinhVienHoc> GetAllSinhVienHoc()
        {
            return SinhVienHoc.GetAllSinhVienHoc();
        }





        public System.Collections.ObjectModel.ObservableCollection<LopHoc> GetAllLopHoc()
        {
            return LopHoc.GetAllLopHoc();
        }


        public void OneWay(int milisecond)
        {
            System.Threading.Thread.Sleep(milisecond);
            int x = int.Parse("");
        }


        public List<LoaiHang> GetAllLoaiHang()
        {
            return null;
        }


        public string Login(string name, string pass)
        {
            return name + pass;
        }


        public string JsonRequest(string request)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            MatHang mathag = json.Deserialize<MatHang>(request);
            return "dsds";
        }


        public List<MatHang> VanhungJson(LoaiHang vanhung)
        {
            return vanhung.MatHangs;
            
        }


        public string TestArray(string name, string age)
        {
           return name+age;
        }


        public string TestArrayObject(Vanhung vanhung, VanHungJson vanhungjson)
        {
            return vanhung.Address;
        }


        public string TestArrayObjectAndString(Vanhung vanhung, string name)
        {
            return name + vanhung.Address;
        }


        public string HelloWord()
        {
            return "Chao ban";
        }


        public int SinhvienInsert(SinhVien sinhvien)
        {
            return SinhVien.InsertSinhvien(sinhvien);
        }
    }
    [DataContract]
    public class FileData
    {
        [DataMember]
        public FileStream fileStream{get;set;}
    }
}
